<?php

/**
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 3 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
 */
/**
 * DatabaseConnection logger class
 * @package Core
 * @subpackage Database
 * @category Interface
 * @author kovacsricsi
 */

namespace Core\Database;

class Log {

	/**
	 * If enabled in config.ini writes a database queries into the a log for debugging.
	 * @access public
	 * @param mixed $connectionClass The connection object
	 * @param string $sqlQuery SQL query to be logged
	 * @return void
	 */
	public static function logQuery($connectionClass, $sqlQuery) {
		$reader = \Util\Config\Cache::getConfig(DIR_ROOT . "/config/Config.ini");
		if ($reader->DATABASE->logQueries) {
			$classNameParts = explode("\\", get_class($connectionClass));
			if ($reader->DATABASE->logQueries == "file"){
				\Core\ErrorHandler\Basic::getInstance()->write_database_query_log($classNameParts[2], $sqlQuery);
			} else {
				\External\FirePHP\Fire::log($classNameParts[2], $sqlQuery);
			}
		}
	}

	/**
	 * Writes an invalid query database error into the database error log.
	 * @access public
	 * @param mixed $connectionClass The connection object
	 * @param int $errorCode Error code of the error
	 * @param string $errorMessage Error message of the error
	 * @param string $badQuery The bad query
	 * @return void
	 */
	public static function logQueryError($connectionClass, $errorCode, $errorMessage, $badQuery) {
		$classNameParts = explode("\\", get_class($connectionClass));
		self::logDatabaseError($classNameParts[2], $errorCode, $errorMessage . " QUERY: " . $badQuery);
	}

	/**
	 * Writes a database type not supported database error into the database error log.
	 * @access public
	 * @param mixed $connectionClass The connection object
	 * @return void
	 */
	public static function logNotSupportedError($connectionClass) {
		$classNameParts = explode("\\", get_class($connectionClass));
		self::logDatabaseError($classNameParts[2], 0, $classNameParts[2] . " not supported in PHP");
	}

	/**
	 * Writes a database connection failed database error into the database error log.
	 * @access public
	 * @param mixed $connectionClass The connection object
	 * @param string $databaseName The name of the database, which had the connection failure
	 * @return void
	 */
	public static function logConnectionError($connectionClass, $databaseName) {
		$classNameParts = explode("\\", get_class($connectionClass));
		self::logDatabaseError($classNameParts[2], 0, "Cannot connect to database : " . $databaseName);
	}

	/**
	 * Writes a database error into the database error log.
	 * @access public
	 * @param string $databaseType
	 * @param integer $errno Error code
	 * @param string $errstr Error message
	 * @return void
	 */
	public static function logDatabaseError($databaseType, $errno, $errstr) {
		$reader = \Util\Config\Cache::getConfig(DIR_ROOT . "/config/Config.ini");
		if ($reader->DATABASE->logErrorsTo == "file") {
			\External\FirePHP\Fire::log($databaseType, $errno ." ". $errstr);
		} else {
			\Core\ErrorHandler\Basic::getInstance()->write_database_error_log($databaseType, $errno, $errstr);
		}

	}

}

?>
